﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;

namespace RH.EBS.Core.Repository.DBUtility
{
    public class SequenceMap
    {
        private static DataTable _dtMap;
        public static string GetSequenceName(string mapName)
        {
            if (_dtMap == null)
            {
                GetMapInfoFromFile();
            }

            DataRow[] dr = _dtMap.Select("MappingName='" + mapName + "'");

            if (dr == null || dr.Length == 0)
            {
                LogWriter.Write(LOG_CATEGORY.SYSTEM, LOG_LEVEL.ERROR, "没有找到映射信息！MappingName=\"" + mapName + "\"");
                return "";
            }

            return dr[0]["SequenceName"].ToString();
        }

        private static void GetMapInfoFromFile()
        {
            _dtMap = new DataTable("SequenceMap");

            _dtMap.Columns.Add("MappingDescription", typeof(string));
            _dtMap.Columns.Add("SequenceName", typeof(string));
            _dtMap.Columns.Add("MappingName", typeof(string));

            try
            {
                _dtMap.ReadXml(HttpContext.Current.Server.MapPath("SEQUENCE_MAPPING.SIN"));
            }
            catch (Exception ex)
            {
                LogWriter.Write(LOG_CATEGORY.SYSTEM, LOG_LEVEL.ERROR, "读取序列映射文件出错！" + ex.ToString());
            }
        }

        public static void RefreshSequenceMapping()
        {
            GetMapInfoFromFile();
        }
    }
}
